#!/bin/sh -e
#create a script to run nac_client automatically

OS=`uname`
if [ "x$OS" = "xLinux" ]; then
    #create nacstarter script in NetworkManager
    STARTER=/etc/NetworkManager/dispatcher.d/60nacstarter
    if [ ! -f $STARTER ]; then
        cp template/nac_client_starter 60nacstarter.txt
        chmod 755 60nacstarter.txt
        mv 60nacstarter.txt /etc/NetworkManager/dispatcher.d/60nacstarter
        chown root:root /etc/NetworkManager/dispatcher.d/60nacstarter
        echo "$STARTER is created!"
    else
        echo "File 60nacstarter already exists!"
        read -p "Do you want to overwrite it?(y or n)" ans
        if [ $ans = "y" ]; then
            cp template/nac_client_starter 60nacstarter.txt
            chmod 755 60nacstarter.txt
            mv 60nacstarter.txt /etc/NetworkManager/dispatcher.d/60nacstarter
            chown root:root /etc/NetworkManager/dispatcher.d/60nacstarter
            echo "$STARTER is created!"
        fi
    fi

    test -d /opt/nac || mkdir -p /opt/nac
    #create configuration file
    NAC_CFILE=/opt/nac/join_ndn.conf
    if [ ! -f $NAC_CFILE ]; then
        cp template/join_ndn.conf /opt/nac
        echo "$NAC_CFILE is created!"
    else
        echo "File $NAC_CFILE already exists!"
        read -p "Do you want to overwrite it?(y or n)" ans2
        if [ $ans2 = "y" ]; then
            cp template/join_ndn.conf /opt/nac
            echo "$NAC_CFILE is created!"
        fi
    fi

    #let user specify the home domain
    read -p "Please specify your home domain.(type 'none' if you don't have)" myhome
    if [ x"$myhome" != "xnone" ]; then
        echo $myhome >> $NAC_CFILE
    fi
fi

